//发布后测试接口（必须用域名及https）
// export const apiBaseUrl = 'https://cdjjyl.com.cn/JianGeSmartServer/'
// 本地运行
export const apiBaseUrl = '/api/'

// 图片上传域名拼接
export const imageBaseUrl = "https://cdjjyl.com.cn/"

// 项目版本  dev测试版   prob正式版
export const edition = "prob"

// 固定的乡村Code
export const communityCode = "2021030891230011";

//公众号appID
export const appId = 'wx2ff544a60dc3e8b8';


const generateUuid = (randomFlag, min, max) => {
	var str = '',
		range = min,
		arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
	//随机产生
	if (randomFlag) {
		range = Math.round(Math.random() * (max - min)) + min;
	}
	for (var i = 0; i < range; i++) {
		var pos = Math.round(Math.random() * (arr.length - 1));
		str += arr[pos]
	}
	return str;
}
// 需要登陆的，都写到这里，否则就是不需要登陆的接口
const methodsToken = [
	'consumer/queryUserInfo',
	'proper/getCommunityByAreaCode',
	'getNoticeByAreaCode',
	'flow/myProcessOwnerInfo',
	'consumer/activity/currency/appletQueryArticle',
	'consumer/activity/currency/articelQueryDetail',
];

const post = (method, data, callback, hideLoading) => {
	//请求头header信息
	var postDataHeader = {
		requestId: generateUuid(false, 32),
		timeStamp: new Date().getTime(),
		applicationId: 'b2b2c-mobile',
		ip: '0.0.0.0'
	};
	//请求头body信息
	var postDataBody = data;
	// 判断token是否存在
	if (methodsToken.indexOf(method) >= 0) {
		// 获取用户token
		let userToken = uni.getStorageSync("userToken");
		if (!userToken) {
			uni.navigateTo({
				url: '/pages/public/login'
			})
			return false;
		} else {
			//将tokenId置于请求头信息
			postDataHeader.tokenId = userToken;
		}
	}
	var postData = {
		header: postDataHeader,
		body: postDataBody
	}
	//如果调用接口不明确指明不显示loading
	if (!hideLoading) {
		uni.showLoading({
			title: '加载中'
		});
	}
	console.log(apiBaseUrl + method)
	uni.request({
		url: apiBaseUrl + method,
		data: postData,
		method: 'POST',
		success: (response) => {
			if (!hideLoading) {
				uni.hideLoading();
			}
			const result = response.data
			// if (result.body && !result.body.status.statusCode === '0') {
			// 	// 登录信息过期或者未登录
			// 	if (result.body.status.errorCode === 'ErrorCode.common.0002' || result.body.status
			// 		.errorCode ===
			// 		'ErrorCode.common.0003' || result.body.status.errorCode === 'ErrorCode.common.0004'
			// 	) {
			// 		uni.removeStorageSync('userToken')
			// 		uni.showToast({
			// 			title: result.body.status.errorDesc,
			// 			icon: 'none',
			// 			duration: 1000,
			// 			complete: function() {
			// 				setTimeout(function() {
			// 					uni.hideToast();
			// 					uni.navigateTo({
			// 						url: '/pages/public/login'
			// 					})
			// 				}, 1000)
			// 			}
			// 		});
			// 	}
			// }
			callback(result);
		},
		complete: () => {
			if (!hideLoading) {
				uni.hideLoading();
			}
		},
		fail: (error) => {
			if (!hideLoading) {
				uni.hideLoading();
			}
			if (error && error.response) {
				showError(error.response);
			}
		},
	});

}

const showError = error => {
	let errorMsg = ''
	switch (error.status) {
		case 400:
			errorMsg = '请求参数错误'
			break
		case 401:
			errorMsg = '未授权，请登录'
			break
		case 403:
			errorMsg = '跨域拒绝访问'
			break
		case 404:
			errorMsg = `请求地址出错: ${error.config.url}`
			break
		case 408:
			errorMsg = '请求超时'
			break
		case 500:
			errorMsg = '服务器内部错误'
			break
		case 501:
			errorMsg = '服务未实现'
			break
		case 502:
			errorMsg = '网关错误'
			break
		case 503:
			errorMsg = '服务不可用'
			break
		case 504:
			errorMsg = '网关超时'
			break
		case 505:
			errorMsg = 'HTTP版本不受支持'
			break
		default:
			errorMsg = error.msg
			break
	}

	uni.showToast({
		title: errorMsg,
		icon: 'none',
		duration: 1000,
		complete: function() {
			setTimeout(function() {
				uni.hideToast();
			}, 1000);
		}
	});
}

// 文件上传
export const uploadFiles = (callback) => {
	uni.chooseImage({
		success: (chooseImageRes) => {
			uni.showLoading({
				title: '上传中...'
			});
			const tempFilePaths = chooseImageRes.tempFilePaths;
			const uploadTask = uni.uploadFile({
				url: apiBaseUrl + 'api.html', //仅为示例，非真实的接口地址
				filePath: tempFilePaths[0],
				fileType: 'image',
				name: 'file',
				headers: {
					'Accept': 'application/json',
					'Content-Type': 'multipart/form-data',
				},
				formData: {
					'method': 'images.upload',
					'upfile': tempFilePaths[0]
				},
				success: (uploadFileRes) => {
					callback(JSON.parse(uploadFileRes.data));
				},
				fail: (error) => {
					if (error && error.response) {
						showError(error.response);
					}
				},
				complete: () => {
					setTimeout(function() {
						uni.hideLoading();
					}, 250);
				}
			});
		}
	});
}

// 上传图片
export const uploadImage = (num, callback) => {
	uni.chooseImage({
		count: num,
		success: (res) => {
			uni.showLoading({
				title: '上传中...'
			});
			let tempFilePaths = res.tempFilePaths
			for (var i = 0; i < tempFilePaths.length; i++) {
				uni.uploadFile({
					url: apiBaseUrl + 'uploadMediaFile',
					filePath: tempFilePaths[i],
					fileType: 'image',
					name: 'files',
					headers: {
						'Accept': 'application/json',
						'Content-Type': 'multipart/form-data',
					},
					formData: {
						'method': 'images.upload',
						'upfile': tempFilePaths[i]
					},
					success: (uploadFileRes) => {
						callback(JSON.parse(uploadFileRes.data));
					},
					fail: (error) => {
						if (error && error.response) {
							showError(error.response);
						}
					},
					complete: () => {
						setTimeout(function() {
							uni.hideLoading();
						}, 250);
					},
				});
			}
		}
	});
}

// 用户登录
export const login = (data, callback, hideLoading) => post('consumer/login', data, callback, hideLoading);


export const userInfoAndAreaInfo = (data, callback, hideLoading) => post('consumer/queryUserInfo', data, callback,
	hideLoading);

// 获取天气
export const getWeather = (data, callback, hideLoading) => post('public/getWeather', data, callback, hideLoading);

// 获取所有乡村
export const getCommunityByAreaCode = (data, callback, hideLoading) => post('proper/getCommunityByAreaCode', data,
	callback, hideLoading);

// 获取首页滚动条配置
export const getNoticeByAreaCode = (data, callback, hideLoading) => post('getNoticeByAreaCode', data, callback,
	hideLoading);

// 查询活动公告
export const inquiryArticle = (data, callback, hideLoading) => post('article/inquiryArticleById', data, callback,
	hideLoading);

// 分页查询资讯
export const inquiryArticlePagination = (data, callback, hideLoading) => post('article/inquiryArticlePagination', data,
	callback,
	hideLoading);

export const myProcessOwnerInfo = (data, callback, hideLoading) => post('flow/myProcessOwnerInfo', data, callback,
	hideLoading);

// 查询党组织列表
export const queryListParty = (data, callback, hideLoading) => post('partyOrganization/getHeadquarters', data,
	callback, hideLoading);

// 查询党组织详情
export const queryMember = (data, callback, hideLoading) => post('partyOrganization/queryPartyOwner', data, callback,
	hideLoading);

// 查询党员信息
export const queryartyMemberList = (data, callback, hideLoading) => post('partyOrganization/queryPartyMember', data,
	callback, hideLoading);

// 老年人高龄津贴申请详情
export const getOldManById = (data, callback, hideLoading) => post('flow/getOldManById', data, callback, hideLoading);

// 最低生活保障申请详情
export const getLifeById = (data, callback, hideLoading) => post('flow/getLifeById', data, callback, hideLoading);

// 困难群众临时救助申请详情
export const getPoorPeopleById = (data, callback, hideLoading) => post('flow/getPoorPeopleById', data, callback,
	hideLoading);

// 根据id查询就业困难人员申请详情
export const getEmploymentById = (data, callback, hideLoading) => post('flow/getEmploymentById', data, callback,
	hideLoading);

// 根据id查询农保申请详情
export const getAgriculturalById = (data, callback, hideLoading) => post('flow/agricultural/getById', data, callback,
	hideLoading);

// 根据id查询生育服务登记申请详情
export const getBirthById = (data, callback, hideLoading) => post('flow/birth/getById', data, callback,
	hideLoading);

// 根据id查询养老保险注销申请详情
export const getCancelById = (data, callback, hideLoading) => post('flow/cancel/getById', data, callback,
	hideLoading);

// 根据id查询居民上户申请详情
export const getHouseholdById = (data, callback, hideLoading) => post('flow/household/getById', data, callback,
	hideLoading);

// 根据id查询医保代交申请详情
export const getMedicalById = (data, callback, hideLoading) => post('flow/medical/getById', data, callback,
	hideLoading);

// 根据id查询养老保险领取申请详情
export const getRegistrationById = (data, callback, hideLoading) => post('flow/registration/getById', data, callback,
	hideLoading);

// 开始流程flow/restart-process
export const startProcess = (data, callback, hideLoading) => post('flow/start-process', data, callback,
	hideLoading);

// 重新提交流程
export const reStartProcess = (data, callback, hideLoading) => post('flow/restart-process', data, callback,
	hideLoading);

//查询问题列表
export const queryQaList = (data, callback, hideLoading) => post('public/queryQaList', data, callback, hideLoading);

// 根据用户ID查询业主信息
export const getOwnerByUserId = (data, callback, hideLoading) => post('owner/getOwnerByUserId', data,
	callback, hideLoading);

//办事指南模糊查询
export const LikeTypeName = (data, callback, hideLoading) => post('public/LikeTypeName', data, callback, hideLoading);

//获取文章详情
export const getArticalDetails = (data, callback, hideLoading) => post('consumer/activity/currency/articelQueryDetail',
	data, callback, hideLoading);

//查询事件上报
export const getEventsOrAdviceLists = (data, callback, hideLoading) => post('event/getEventsOrAdviceLists', data,
	callback, hideLoading);

// 查询事件上报类型
export const getTagsByType = (data, callback, hideLoading) => post('event/getTagsByType', data, callback, hideLoading);

//添加事件上报
export const addEvent = (data, callback, hideLoading) => post('event/addEvent', data, callback, hideLoading);

//事件上报详情
export const getDetailById = (data, callback, hideLoading) => post('event/getDetailById', data, callback, hideLoading);

// 查询流程图
export const getProcessDiagram = (data, callback, hideLoading) => post('flow/getProcessDiagram', data, callback,
	hideLoading);

// 管理快递地址
export const managerExpressAddress = (data, callback, hideLoading) => post('express/managerExpressAddress', data,
	callback, hideLoading);

// 查询快递地址
export const queryExpressAddress = (data, callback, hideLoading) => post('express/queryUserAddressPage', data, callback,
	hideLoading);

// 查询价格
export const queryExpressPrice = (data, callback, hideLoading) => post('express/queryPrice', data, callback,
	hideLoading);

// 下单
export const expressPlaceAnOrder = (data, callback, hideLoading) => post('express/placeAnOrder', data, callback,
	hideLoading);

// 取消订单
export const cancelExpress = (data, callback, hideLoading) => post('express/cancelExpress', data, callback, hideLoading);

// 根据ID查询订单地址
export const queryExpressAddressById = (data, callback, hideLoading) => post('express/queryExpressAddressById', data,
	callback, hideLoading);

// 查询快递记录
export const queryExpressRecordPage = (data, callback, hideLoading) => post('express/queryExpressRecordPage', data,
	callback, hideLoading);

// 根据ID查询物流示例
export const queryExpressRecordById = (data, callback, hideLoading) => post('express/queryExpressRecordById', data,
	callback, hideLoading);

// 查询物流
export const queryLogisticsInfo = (data, callback, hideLoading) => post('express/searchRoutes', data, callback,
	hideLoading);

// 查询服务网点
export const queryExpressService = (data, callback, hideLoading) => post('express/queryExpressService', data, callback,
	hideLoading);

// 添加登记记录
export const addRegister = (data, callback, hideLoading) => post('register/add', data, callback,
	hideLoading);

export const queryRegisterList = (data, callback, hideLoading) => post('register/queryList', data, callback,
	hideLoading);

export const queryRegisterById = (data, callback, hideLoading) => post('register/queryDetail', data, callback,
	hideLoading);

export const getRegisterHandleDetail = (data, callback, hideLoading) => post('register/getRegisterHandleDetail', data,
	callback,
	hideLoading);

// 增加或修改我的就业创业登记基本信息
export const addOrUpdateResume = (data, callback, hideLoading) => post('employment/insertOrUpdate', data,
	callback, hideLoading);

// 根据用户ID查询我的就业创业登记信息
export const queryResumeByUserId = (data, callback, hideLoading) => post('employment/queryByUserId', data,
	callback, hideLoading);

// 修改或添加创业就业教育信息
export const addOrUpdateEducation = (data, callback, hideLoading) => post('employment/updateStudy', data, callback,
	hideLoading);

// 修改或添加创业就业教育信息
export const addOrUpdateWork = (data, callback, hideLoading) => post('employment/updateWork', data, callback,
	hideLoading);

// 修改或添加创业就业附件信息
export const addOrUpdateFiles = (data, callback, hideLoading) => post('employment/updateEnclosure', data, callback,
	hideLoading);

// 修改或添加创业就业附件信息
export const inquiryEpidemic = (data, callback, hideLoading) => post('epidemic/inquiryEpidemic', data, callback,
	hideLoading);

//查询全部培训信息
export const queryTrainInfoPage = (data, callback, hideLoading) => post('job/queryTrainInfoPage', data, callback,
	hideLoading);

//查询全部岗位信息
export const queryPostInfoPage = (data, callback, hideLoading) => post('job/queryPostInfoPage', data, callback,
	hideLoading);

